
package com.borqs.music.ui;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

import com.borqs.music.R;

public abstract class GestureTabActivity extends TabActivity {

    private static final int SWIPE_MIN_DISTANCE = 150;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 150;
    private GestureDetector mGestureDetector;

    private int mCurrentView = 0;
    private static int mMaxTabIndex = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_music_tabhost);
        mMaxTabIndex = getTabCount();
        mGestureDetector = new GestureDetector(new MyGestureDetector());
        addTabs();
    }

    protected abstract void addTabs();

    protected View createIndicator(final int textId, boolean showSeparateline) {
        View view = LayoutInflater.from(this).inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(textId);
        if (showSeparateline) {
            view.findViewById(R.id.separate_line).setVisibility(View.VISIBLE);
        }
        return view;
    }

    private class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentView == mMaxTabIndex) {
                        return false;
                    } else {
                        mCurrentView++;
                    }
                    tabHost.setCurrentTab(mCurrentView);
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (mCurrentView == 0) {
                        return false;
                    } else {
                        mCurrentView--;
                    }
                    tabHost.setCurrentTab(mCurrentView);
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        if (mGestureDetector.onTouchEvent(event)) {
            event.setAction(MotionEvent.ACTION_CANCEL);
        }

        return super.dispatchTouchEvent(event);
    }

    public abstract int getTabCount();
}
